<?php
namespace app\common\model\base;

use think\Model;
use think\Db;

/*************************************************
*ClassName:     CommonModel
*Description:   公用模型
*Others:      
*************************************************/
class CommonModel extends Model
{
    /**
     * setTableName 增加
     * @xiao
     * @DateTime 2016-07-31T07:38:20+0800
     * @param    string                   $table_name 表名
     * @return   void
     */
    static public function setTableName($table_name)
    {

    }
    /**
     * setTableName 增加
     * @xiao
     * @DateTime 2016-07-31T07:38:20+0800
     * @return   string
     */
    static public function getClassName()
    {

    }
    /**
     * add 增加
     * @xiao
     * @DateTime 2016-07-31T07:38:20+0800
     * @param    array                   $data 输入数据
     * @return   array                   [error_code, error_msg, id ]
     */
    static public function add($data = [])
    {
        $class_name = get_called_class();
        $add_id = Db::name($class_name::$table_name)->insertGetId($data);
        if ($add_id) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['id'] = $add_id;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '添加失败';
        }
        return $result;
    }
    /**
     * edit 编辑
     * @xiao
     * @DateTime 2016-07-31T07:38:20+0800
     * @param    array                   $where 条件
     * @param    array                   $data  编辑数据
     * @return   array                   [error_code, error_msg]
     */
    static public function edit($where, $data = [])
    {
        $class_name = get_called_class();
        $res = Db::name($class_name::$table_name)->where($where)->update($data);
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '编辑失败';
        }
        return $result;
    }
    /**
     * del 删除
     * @xiao
     * @DateTime 2016-07-31T07:38:20+0800
     * @param    array                   $data 输入数据
     * @return   array                   [error_code, error_msg]
     */
    static public function del($data = [])
    {
        $class_name = get_called_class();
        if (is_array($data))
            $res = Db::name($class_name::$table_name)->where($data)->delete();
        elseif (is_numeric($data))
            $res = Db::name($class_name::$table_name)->delete($data);
        else {
            $result['error_code'] = 2;
            $result['error_msg'] = '输入数据类型错误';
            return $result;
        }
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '删除失败';
        }
        return $result;
    }
    /**
     * getOneRow 查询一条数据
     * @xiao karl
     * @DateTime 2016-11-22T14:45:03+0800
     * @param    array                   $where 查询条件
     * @param    array                   $field 字段
     * @return   array                   [error_code, error_msg, data ]
     */
    static public function getOneRow($where = [],$field = [])
    {
        $class_name = get_called_class();
        $res = Db::name($class_name::$table_name)->field($field)->where($where)->find();
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $res;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '查询失败';
        }
        return $result;
    }
    /**
     * getRow 查询多条数据
     * @xiao karl
     * @DateTime 2016-11-22T14:45:03+0800
     * @param    array                   $where 查询条件
     * @return   array                   [error_code, error_msg, data ]
     */
    static public function getRows($where = [])
    {
        $class_name = get_called_class();
        $res = Db::name($class_name::$table_name)->where($where)->select();
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $res;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '查询失败';
        }
        return $result;
    }
    /**
     * getRowsPage 查询多条数据
     * @xiao
     * @param    array                   $where 查询条件
     * @param    integer                 $page  当前页
     * @param    string                  $sort   排列顺序
     * @param    string                  $order  排序条件
     * @param    array                   $url   查询条件
     * @param    integer                 $page_num   每页显示数量
     * @return   array                   [error_code, error_msg, data=> [] ]
     */
    static public function getRowsPage($where = [], $page = 1, $sort = 'id', $order = 'desc', $url = [] ,$page_num = 20)
    {
        $class_name = get_called_class();
        if (empty($page))
            $res = Db::name($class_name::$table_name)->where($where)->order($sort.' '.$order)->paginate($page_num, false , ['query' => $url]);
        else
            $res = Db::name($class_name::$table_name)->where($where)->order($sort.' '.$order)->page($page, $page_num)->select();
        if ($res) {
            $result['error_code'] = 0;
            $result['error_msg'] = '';
            $result['data'] = $res;
        }else{
            $result['error_code'] = 1;
            $result['error_msg'] = '查询失败';
        }
        return $result;
    }
}